package l1j.server.server.serverpackets;

import java.io.IOException;
import java.util.List;

import l1j.opqlo.Server.L1World.L1World;
import l1j.server.server.model.L1Object;
import l1j.server.server.model.Instance.L1ItemInstance;
import l1j.server.server.model.Instance.L1NpcInstance;
import l1j.server.server.model.Instance.L1PcInstance;
import l1j.server.server.templates.L1Item;

public class S_UpdateItemList_Cover extends ServerBasePacket {

	/**
	 * 武器轉換類型
	 */
	public S_UpdateItemList_Cover(final int objId, final L1PcInstance pc,
			final int type) {
		this.writeC(S_OPCODE_SHOWSHOPBUYLIST);
		this.writeD(objId);

		final L1Object npcObj = L1World.getInstance().findObject(objId);
		if (!(npcObj instanceof L1NpcInstance)) {
			this.writeH(0);
			return;
		}
		pc._coverType = type;
		final List<L1ItemInstance> shopItems = pc.getInventory()
				.getCoverWeapon(type);

		this.writeH(shopItems.size());

		// L1ItemInstanceのgetStatusBytesを利用するため

		for (int i = 0; i < shopItems.size(); i++) {
			final L1ItemInstance shopItem = shopItems.get(i);
			final L1Item item = shopItem.getItem();
			this.writeD(i);
			this.writeH(item.getGfxId());
			if (item.getType2() == 0) {
				this.writeD(0);
			} else {
				this.writeD(500);
			}
			this.writeS(shopItem.getLogName());
			final byte[] status = shopItem.getStatusBytes();
			this.writeC(status.length);
			for (final byte b : status) {
				this.writeC(b);
			}

		}
		this.writeH(0x07); // 0x00:kaimo 0x01:pearl 0x07:adena
	}

	@Override
	public byte[] getContent() throws IOException {
		return this._bao.toByteArray();
	}
}
